package com.yunxi.service.ups.dubbo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yunxi.core.base.Constant;
import com.yunxi.core.util.HttpUtil;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.model.eai.EaiLog;
import com.yunxi.rocketmq.producer.EaiLogsProducer;
import com.yunxi.ups.pojo.ActivityVO;
import com.yunxi.ups.pojo.PreApproveVO;
import com.yunxi.ups.pojo.RepairOrderVO;
import com.yunxi.ups.service.UpsDubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.Date;
import java.util.Map;

/**
 * Created by Administrator on 2019/9/1.
 */
public class UpsDubboServiceImpl implements UpsDubboService {

    protected static final Logger LOGGER = LoggerFactory.getLogger(UpsDubboServiceImpl.class);
    @Autowired
    private EaiLogsProducer eaiLogsProducer;
    @Value("${esb.switch}")
    private String esbSwitch;
    @Value("${UPS.URL}")
    private String UPS_URL;
   /*
    @Override
    ApiResult sendPartsPricePMS2UPS(PartsPriceVO order) {
        String partsPrice = JSONObject.toJSONString(order);
        ApiResult apiResult=null;
        EaiLog eaiLog=new EaiLog();
        eaiLog.setPmsEaiContent(partsPrice);
        eaiLog.setPmsEaiText(partsPrice);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("UPS维修系统发送渠道商配件库存价格信息给UPS工单系统");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(Constant.PMS_UPS_PART_PRICE);
        if (esbSwitch.equals("false")) {
            return ResponseUtil.ok();
        }
        try{
            Map map = HttpUtil.sendPost(partsPrice, Constant.PMS_UPS_PART_PRICE, Constant.PMS, UPS_URL);
            eaiLog.setRequestId(String.valueOf(map.get("requestId")));
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setEsbResultContent(JSONObject.toJSONString(map));
            Object object = map.get("header");
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(object));
            String statusFlag=String.valueOf(jsonObject.get("statusFlag"));
            if (statusFlag.equals("0")){//这里只对ESB的处理状态做判断
                //处理失败，更状态位失败
                eaiLog.setDisposeStatus(2);
                apiResult= ResponseUtil.fail();
            }else {
                //将返回的数据返给PMS
                JSONObject jsonResponse = JSONObject.parseObject(String.valueOf(map.get("response")));
                if (Integer.parseInt(String.valueOf(jsonResponse.get("code")))!=200){
                    //处理失败，更状态位失败
                    eaiLog.setDisposeStatus(2);
                    apiResult=ResponseUtil.fail();
                }else {
                    apiResult = ResponseUtil.ok();
                }
            }
        }catch (Exception e){
            //处理失败，更状态位失败
            eaiLog.setDisposeStatus(2);
            LOGGER.error(e.getMessage(),e);
            LOGGER.error("调用ESB系统失败===========");
            apiResult=ResponseUtil.fail();
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        }catch (Exception e){
            LOGGER.error(e.getMessage(),e);
            apiResult=ResponseUtil.fail();
        }
        return apiResult;
    }*/

  /*  @Override
    public ApiResult sendThreePacksOfWarnPMS2UPS(ThreePacksOfWarnVO order) {
        String orders = JSONObject.toJSONString(order);
        ApiResult apiResult=null;
        EaiLog eaiLog=new EaiLog();
        eaiLog.setPmsEaiContent(orders);
        eaiLog.setPmsEaiText(orders);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("UPS维修系统发送三包预警规则给UPS工单系统");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(Constant.PMS_UPS_THREE_PACK_WARN);
        if (esbSwitch.equals("false")) {
            return ResponseUtil.ok();
        }
        try{
            Map map = HttpUtil.sendPost(orders, Constant.PMS_UPS_THREE_PACK_WARN, Constant.PMS, UPS_URL);
            eaiLog.setRequestId(String.valueOf(map.get("requestId")));
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setEsbResultContent(JSONObject.toJSONString(map));
            Object object = map.get("header");
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(object));
            String statusFlag=String.valueOf(jsonObject.get("statusFlag"));
            if (statusFlag.equals("0")){//这里只对ESB的处理状态做判断
                //处理失败，更状态位失败
                eaiLog.setDisposeStatus(2);
                apiResult= ResponseUtil.fail();
            }else {
                //将返回的数据返给PMS
                JSONObject jsonResponse = JSONObject.parseObject(String.valueOf(map.get("response")));
                if (Integer.parseInt(String.valueOf(jsonResponse.get("code")))!=200){
                    //处理失败，更状态位失败
                    eaiLog.setDisposeStatus(2);
                    apiResult=ResponseUtil.fail();
                }else {
                    apiResult = ResponseUtil.ok();
                }
            }
        }catch (Exception e){
            //处理失败，更状态位失败
            eaiLog.setDisposeStatus(2);
            LOGGER.error(e.getMessage(),e);
            LOGGER.error("调用ESB系统失败===========");
            apiResult=ResponseUtil.fail();
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        }catch (Exception e){
            LOGGER.error(e.getMessage(),e);
            apiResult=ResponseUtil.fail();
        }
        return apiResult;
    }*/

    @Override
    public ApiResult sendActivityPMS2UPS(ActivityVO order) {
        String orders = JSONObject.toJSONString(order);
        ApiResult apiResult=null;
        EaiLog eaiLog=new EaiLog();
        eaiLog.setPmsEaiContent(orders);
        eaiLog.setPmsEaiText(orders);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("UPS维修系统发送服务活动信息给UPS工单系统");
        eaiLog.setSourceSystem(Constant.UPSPARTS);
        eaiLog.setServiceName(Constant.PMS_UPS_ACTIVITY);
        if (esbSwitch.equals("false")) {
            return ResponseUtil.ok();
        }
        try{
            Map map = HttpUtil.sendPost(orders, Constant.PMS_UPS_ACTIVITY, Constant.UPSPARTS, UPS_URL);
            eaiLog.setRequestId(String.valueOf(map.get("requestId")));
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setEsbResultContent(JSONObject.toJSONString(map));
            eaiLog.setUrl(UPS_URL);
            Object object = map.get("header");
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(object));
            String statusFlag=String.valueOf(jsonObject.get("statusFlag"));
            if (statusFlag.equals("0")){//这里只对ESB的处理状态做判断
                //处理失败，更状态位失败
                eaiLog.setDisposeStatus(2);
                apiResult= ResponseUtil.fail(400,"ESB错误!");
            }else {
                //将返回的数据返给PMS
                JSONObject jsonResponse = JSONObject.parseObject(String.valueOf(map.get("response")));
                if (Integer.parseInt(String.valueOf(jsonResponse.get("status")))!=1){
                    //处理失败，更状态位失败
                    eaiLog.setDisposeStatus(2);
                    apiResult=ResponseUtil.fail(400,String.valueOf(jsonResponse.get("errorMsg")));
                }else {
                    apiResult = ResponseUtil.ok();
                }
            }
        }catch (Exception e){
            //处理失败，更状态位失败
            eaiLog.setDisposeStatus(2);
            LOGGER.error(e.getMessage(),e);
            LOGGER.error("调用ESB系统失败===========");
            apiResult=ResponseUtil.fail();
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        }catch (Exception e){
            LOGGER.error(e.getMessage(),e);
            apiResult=ResponseUtil.fail();
        }
        return apiResult;
    }

    @Override
    public ApiResult sendPreApprovePMS2UPS(PreApproveVO order) {
        String orders = JSONObject.toJSONString(order);
        ApiResult apiResult=null;
        EaiLog eaiLog=new EaiLog();
        eaiLog.setPmsEaiContent(orders);
        eaiLog.setPmsEaiText(orders);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("UPS维修系统发送预授权申请单给UPS工单系统");
        eaiLog.setSourceSystem(Constant.UPSPARTS);
        eaiLog.setServiceName(Constant.PMS_UPS_PRE_APPROVE);
        if (esbSwitch.equals("false")) {
            return ResponseUtil.ok();
        }
        try{
            Map map = HttpUtil.sendPost(orders, Constant.PMS_UPS_PRE_APPROVE, Constant.UPSPARTS, UPS_URL);
            eaiLog.setRequestId(String.valueOf(map.get("requestId")));
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setEsbResultContent(JSONObject.toJSONString(map));
            eaiLog.setUrl(UPS_URL);
            Object object = map.get("header");
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(object));
            String statusFlag=String.valueOf(jsonObject.get("statusFlag"));
            if (statusFlag.equals("0")){//这里只对ESB的处理状态做判断
                //处理失败，更状态位失败
                eaiLog.setDisposeStatus(2);
                apiResult= ResponseUtil.fail(400,"ESB错误!");
            }else {
                //将返回的数据返给PMS
                JSONObject jsonResponse = JSONObject.parseObject(String.valueOf(map.get("response")));
                if (Integer.parseInt(String.valueOf(jsonResponse.get("status")))!=1){
                    //处理失败，更状态位失败
                    eaiLog.setDisposeStatus(2);
                    apiResult=ResponseUtil.fail(400,String.valueOf(jsonResponse.get("errorMsg")));
                }else {
                    apiResult = ResponseUtil.ok();
                }
            }
        }catch (Exception e){
            //处理失败，更状态位失败
            eaiLog.setDisposeStatus(2);
            LOGGER.error(e.getMessage(),e);
            LOGGER.error("调用ESB系统失败===========");
            apiResult=ResponseUtil.fail();
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        }catch (Exception e){
            LOGGER.error(e.getMessage(),e);
            apiResult=ResponseUtil.fail();
        }
        return apiResult;
    }

    /*@Override
    public ApiResult sendClaimCheckoutPMS2UPS(ClaimCheckoutVO order) {
        String orders = JSONObject.toJSONString(order);
        ApiResult apiResult=null;
        EaiLog eaiLog=new EaiLog();
        eaiLog.setPmsEaiContent(orders);
        eaiLog.setPmsEaiText(orders);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("UPS维修系统发送索赔合理性校验规则给UPS工单系统");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(Constant.PMS_UPS_CLAIM_CKECKOUT);
        if (esbSwitch.equals("false")) {
            return ResponseUtil.ok();
        }
        try{
            Map map = HttpUtil.sendPost(orders, Constant.PMS_UPS_CLAIM_CKECKOUT, Constant.PMS, UPS_URL);
            eaiLog.setRequestId(String.valueOf(map.get("requestId")));
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setEsbResultContent(JSONObject.toJSONString(map));
            Object object = map.get("header");
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(object));
            String statusFlag=String.valueOf(jsonObject.get("statusFlag"));
            if (statusFlag.equals("0")){//这里只对ESB的处理状态做判断
                //处理失败，更状态位失败
                eaiLog.setDisposeStatus(2);
                apiResult= ResponseUtil.fail();
            }else {
                //将返回的数据返给PMS
                JSONObject jsonResponse = JSONObject.parseObject(String.valueOf(map.get("response")));
                if (Integer.parseInt(String.valueOf(jsonResponse.get("code")))!=200){
                    //处理失败，更状态位失败
                    eaiLog.setDisposeStatus(2);
                    apiResult=ResponseUtil.fail();
                }else {
                    apiResult = ResponseUtil.ok();
                }
            }
        }catch (Exception e){
            //处理失败，更状态位失败
            eaiLog.setDisposeStatus(2);
            LOGGER.error(e.getMessage(),e);
            LOGGER.error("调用ESB系统失败===========");
            apiResult=ResponseUtil.fail();
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        }catch (Exception e){
            LOGGER.error(e.getMessage(),e);
            apiResult=ResponseUtil.fail();
        }
        return apiResult;
    }*/

    @Override
    public ApiResult sendRepairOrderPMS2UPS(RepairOrderVO order) {
        String orders = JSONObject.toJSONString(order);
        ApiResult apiResult=null;
        EaiLog eaiLog=new EaiLog();
        eaiLog.setPmsEaiContent(orders);
        eaiLog.setPmsEaiText(orders);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("UPS维修系统发送工单领料信息给UPS工单系统");
        eaiLog.setSourceSystem(Constant.UPSPARTS);
        eaiLog.setServiceName(Constant.PMS_UPS_REPAIR_ORDER);
        if (esbSwitch.equals("false")) {
            return ResponseUtil.ok();
        }
        try{
            Map map = HttpUtil.sendPost(orders, Constant.PMS_UPS_REPAIR_ORDER, Constant.UPSPARTS, UPS_URL);
            eaiLog.setRequestId(String.valueOf(map.get("requestId")));
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setEsbResultContent(JSONObject.toJSONString(map));
            eaiLog.setUrl(UPS_URL);
            Object object = map.get("header");
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(object));
            String statusFlag=String.valueOf(jsonObject.get("statusFlag"));
            if (statusFlag.equals("0")){//这里只对ESB的处理状态做判断
                //处理失败，更状态位失败
                eaiLog.setDisposeStatus(2);
                apiResult= ResponseUtil.fail(400,"ESB错误!");
            }else {
                //将返回的数据返给PMS
                JSONObject jsonResponse = JSONObject.parseObject(String.valueOf(map.get("response")));
                if (Integer.parseInt(String.valueOf(jsonResponse.get("status")))!=1){
                    //处理失败，更状态位失败
                    eaiLog.setDisposeStatus(2);
                    apiResult=ResponseUtil.fail(400,String.valueOf(jsonResponse.get("errorMsg")));
                }else {
                    apiResult = ResponseUtil.ok();
                }
            }
        }catch (Exception e){
            //处理失败，更状态位失败
            eaiLog.setDisposeStatus(2);
            LOGGER.error(e.getMessage(),e);
            LOGGER.error("调用ESB系统失败===========");
            apiResult=ResponseUtil.fail();
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        }catch (Exception e){
            LOGGER.error(e.getMessage(),e);
            apiResult=ResponseUtil.fail();
        }
        return apiResult;
    }
}
